package org.redr.canvas;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.redr.widgetGUI.settings.CoreSettings;

import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QDialogButtonBox;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QSizePolicy;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.webkit.QWebView;

public class AboutDlg extends QDialog {
	
	private QWebView about;

	public AboutDlg(){
		super();
		CoreSettings settings = RedRApplication.coreSettings();
		File parentDir = RedRApplication.parentDir;
		QWidget topWidgetBox = new QWidget(this);
		topWidgetBox.setLayout(new QVBoxLayout());
		// TODO check syntax and set setWindowFlags();
		
		QPixmap logoImage = new QPixmap(OSPath.join(parentDir, "coreIcons", "splash.png").getAbsolutePath());
		QLabel logo	=	new QLabel(this);
		logo.setPixmap(logoImage);
		String info = settings.getSetting("REDRVERSION");
		
		about = new QWebView(this);
		about.setSizePolicy(QSizePolicy.Policy.MinimumExpanding, QSizePolicy.Policy.MinimumExpanding);
		about.setMinimumHeight(190);
		
		about.setHtml(String.format("<h2>%(NAME)s %(REDRVERSION)s</h2>Type: %(TYPE)s " + 
				"<br> Revision: %(SVNVERSION)s " + 
				"<br> Build Time: %(DATE)s <br> " + 
				"R Version: %(RVERSION)s <br>" +
				"<h3>Red-R Core Development Team (<a href=\"http://www.red-r.org\">Red-R.org</a>)</h3>", info));
		QPushButton licenceButton = new QPushButton("Licence", this);
		licenceButton.clicked.connect(this, "showLicence(boolean)");
		QDialogButtonBox b = new QDialogButtonBox(this);
        b.setCenterButtons(true);
        this.layout().addWidget(b);
        QPushButton butt = b.addButton(QDialogButtonBox.StandardButton.Close);
        butt.clicked.connect(this, "accept()");
    }
	
	public void showLicence(boolean b){
		FileReader fread;
		try {
			fread = new FileReader(OSPath.join(RedRApplication.parentDir, "licence.txt"));

	        BufferedReader bufRead	=	new BufferedReader(fread);
			String text 	= 	bufRead.toString();
	        fread.close();
	        
	        about.setHtml("<pre>"+text+"</pre>");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
